package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.ClusterInstance;
import com.ffcs.crmd.tsp.admin.service.IClusterInstanceService;
import com.ffcs.crmd.tsp.admin.service.ITspClientService;
import com.ffcs.crmd.tsp.api.dto.client.TspClusterInstance;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.client.enu.TspResultType;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.PropertiesUtil;

/**
 * 
 * 功能说明:
 *
 * @author FFCS-ZHONGFUHUA
 * 
 * @since 
 *
 */
@Service
public class ClusterInstanceServiceImpl implements IClusterInstanceService {
    
    @Autowired
    private ITspClientService tspClientService;
    
    @Override
    public List<ClusterInstance> query(String clusterName) throws TspException {
        try {
            List<ClusterInstance> clusterInstanceList = new ArrayList<ClusterInstance>();
            Properties properties = PropertiesUtil.getInstance().loadWeb("conf/cluster.properties");
            String serverAddrs = properties.getProperty(clusterName);
            String[] addrList = serverAddrs.split(";");
            for (String addr : addrList) {
                if (StringUtils.isNotBlank(addr)) {
                    ClusterInstance clusterInstance = new ClusterInstance();
                    clusterInstance.setIpAddress(addr);
                    clusterInstance.setClusterName(clusterName);
                    
                    TspClusterInstance tspClusterInstanceVO = new TspClusterInstance();
                    tspClusterInstanceVO.setIpAddress(addr);
                    TspClient tspClient = tspClientService.getClient(clusterName);
                    TspResult<TspClusterInstance> requestResult = tspClient.queryClusterInstance(tspClusterInstanceVO);
                    if (requestResult.getTspResultType() == TspResultType.SUCCESS) {
                        BeanUtils.applyIf(tspClusterInstanceVO, requestResult.getRetVo().getObject());
                        clusterInstance.setInstanceId(tspClusterInstanceVO.getInstanceId());
                        clusterInstance.setRunning((short) 1);
                    }
                    clusterInstanceList.add(clusterInstance);
                }
            }
            return clusterInstanceList;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public com.ctg.itrdc.platform.common.entity.PageInfo<ClusterInstance> queryPage(String clusterName, int pageNumber, int pageSize) throws TspException {
        try {
            List<ClusterInstance> clusterInstanceList = query(clusterName);
            int totalCounts = clusterInstanceList.size();
            int first = PageInfo.getPageBeginSize(pageSize, pageNumber);
            int end = PageInfo.getPageEndSize(first, pageSize, totalCounts);
            clusterInstanceList = clusterInstanceList.subList(first, end);
            PageInfo<ClusterInstance> pageInfo = new PageInfo<ClusterInstance>(clusterInstanceList);
            pageInfo.calcAndJudge(pageSize, pageNumber, totalCounts, clusterInstanceList);
            
            com.ctg.itrdc.platform.common.entity.PageInfo<ClusterInstance> rpageInfo = new com.ctg.itrdc.platform.common.entity.PageInfo<ClusterInstance>();
            BeanUtils.applyIf(rpageInfo, pageInfo);
            return  rpageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
}
